#include "../include/config.h"

// Config
#include "../include/storage_engine/HappyEngineConfig.h"

// std and boost
#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/lexical_cast.hpp>


#include "../include/Async/server.hpp"

// common 
#include "../include/storage_engine/HappyEngineOption.h"


// StorageEngine
#include "../include/storage_engine/item_data.h"
#include "../include/storage_engine/item.h"
#include "../include/storage_engine/bdb.h"


#define HAPPYENGINE 1


using namespace HappyDB::StorageEngine;

static HappyDB::Common::CHappyEngineOption   g_option;



boost::function0<void> console_ctrl_function;
BOOL WINAPI console_ctrl_handler(DWORD ctrl_type)
{
  switch (ctrl_type)
  {
  case CTRL_C_EVENT:
  case CTRL_BREAK_EVENT:
  case CTRL_CLOSE_EVENT:
  case CTRL_SHUTDOWN_EVENT:
    console_ctrl_function();
    return TRUE;
  default:
    return FALSE;
  }
}


#ifdef _DEBUG
#define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
#endif
inline void EnableMemLeakCheck()
{
	_CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);
}


int main(int argc, char* argv[])
{
	try
	{



		//EnableMemLeakCheck();



		//if (g_option.init(argc, argv) == 0)
		//	return 0;
		//  

		//HappyDB::StorageEngine::CBDB   g_bdb(&g_option);

		//// init bdb
		//g_bdb.BDBSettingsInit();
		//g_bdb.BDBEnvInit();
		//g_bdb.BDBOpen();


		//// item op
		//HappyDB::StorageEngine::CItem<HappyDB::StorageEngine::CBDB>  it(&g_bdb, g_option.m_settings.item_buf_size);
		//it.ItemGet("111", 3);
		//


		//// Initialise server.
		//std::size_t num_threads = boost::lexical_cast<std::size_t>(g_option.m_settings.num_threads);
		//boost::format port("%1%");
		//port % g_option.m_settings.port;

		//http::server3::server s("127.0.0.1", port.str().c_str(), ".", num_threads);

		//// Set console control handler to allow server to be stopped.
		//console_ctrl_function = boost::bind(&http::server3::server::stop, &s);
		//SetConsoleCtrlHandler(console_ctrl_handler, TRUE);

		//// Run the server until stopped.
		//s.run();


		//// close bdb
		//g_bdb.BDBCheckpoint();
		//g_bdb.BDBClose();
		//g_bdb.BDBEnvClose();













		if (argc != 5)
		{
		  std::cerr << "Usage: http_server <address> <port> <threads> <doc_root>\n";
		  std::cerr << "  For IPv4, try:\n";
		  std::cerr << "    http_server 0.0.0.0 80 1 .\n";
		  std::cerr << "  For IPv6, try:\n";
		  std::cerr << "    http_server 0::0 80 1 .\n";
		  return 1;
		}

		// Initialise server.
		std::size_t num_threads = boost::lexical_cast<std::size_t>(argv[3]);
		http::server3::server s(argv[1], argv[2], argv[4], num_threads);

		// Set console control handler to allow server to be stopped.
		console_ctrl_function = boost::bind(&http::server3::server::stop, &s);
		SetConsoleCtrlHandler(console_ctrl_handler, TRUE);

		// Run the server until stopped.
		s.run();



	} 
	catch(DbException &e) 
	{
		std::cerr << "Exception : " << e.what() << endl; 
	}
	catch (std::exception& e)
	{
		std::cerr << "exception: " << e.what() << "\n";
	}

	return 0;
}

